/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

define(['adminapp', 'jquery-flot', 'jquery-flot-pie'], function ($app) {
    $app.navactive('vote_index');


    var $pies = $('div[data-toggle="pie-chart"]');
    if ($pies.length > 0) {
        $pies.each(function () {
            var $this = $(this);
            var data = $this.data();
            var labels = data.labels.split('{}');
            $.get(data.url, function (rst) {
                if (rst.status == 'OK') {
                    var data = rst.data;
                    var length = data.length;
                    var chart_data = [];
                    for(var i = 0; i < length ; i ++){
                        chart_data[i] = {
                            label: labels[i],
                            data : data[i]['choose_times']
                        }
                    }
                    $.plot($this, chart_data, {
                        series: {
                            pie: {
                                show  : true,
                                radius: 1,
                                label : {
                                    show      : true,
                                    radius    : 3 / 4,
                                    formatter : function (label, series) {
                                        return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + Math.round(series.percent) + '%</div>';
                                    },
                                    background: {
                                        opacity: 0.5,
                                        color  : '#000'
                                    }
                                }
                            }
                        },
                        legend: {
                            show: false
                        }
                    });
                }
            });

        });
    }

});